<!DOCTYPE html>
<!-- ***************************************************************************
   CANopen Graphical User interface
   Copyright (C) 2012 Janez Paternoster

   This Source Code Form is subject to the terms of the Mozilla Public
   License, v. 2.0. If a copy of the MPL was not distributed with this file,
   You can obtain one at http://mozilla.org/MPL/2.0/.
**************************************************************************** -->
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>CANopen</title>
<style type="text/css">
body{
   background-color: rgb(165, 186, 221);
}
body > div{
   float: left;
   padding: 5px;
}
#EMCYlogDiv{
   width: 380px;
}
footer{
   display: block;
   clear: left;
}
fieldset{
   position: relative;
}
input[type="text"]{
   position: absolute;
   left: 180px;
   width: 140px;
}
textarea{
   width: 310px;
   height: 411px;
   font-size: 12px;
}
.oneBut > input[type="button"]{
   width: 318px;
}
.twoBut > input[type="button"]{
   width: 153px;
}
.threeBut > input[type="button"]{
   width: 98px;
}
#SDOData{
   height: 16px;
}

</style>
<script type="text/javascript" src="lib/CANopen.js"></script>
<script type="text/javascript">
   //<![CDATA[
   "use strict";

   var CO;
   var CANLog;
   var CANLogCallbackID;

   function SDORead(){
      var statusLine = document.getElementById("SDOStatus").firstChild;
      statusLine.nodeValue = "Read requested ...";
      CO.SDORead(document.getElementById("SDOAddr").value, function(response){
         var OK = response.search("OK");
         if(OK > 0){
            document.getElementById("SDOData").value = response.slice(OK+4);
            statusLine.nodeValue = response.slice(0, OK+2);
         }
         else{
            statusLine.nodeValue = response;
         }
      });
   }

   function SDOWrite(){
      var statusLine = document.getElementById("SDOStatus").firstChild;
      statusLine.nodeValue = "Write requested ...";
      CO.SDOWrite(document.getElementById("SDOAddr").value, document.getElementById("SDOData").value, function(response){
         var OK = response.search("OK");
         if(OK > 0){
            statusLine.nodeValue = response.slice(0, OK+2);
         }
         else{
            statusLine.nodeValue = response;
         }
      });
   }

   function logBoundary(logDate, textBuf){
      if(logDate){
         textBuf.push(logDate.toLocaleString());
      }
      else{
         var ta = document.getElementById("CANLog");
         ta.value = textBuf.join("\n");
         ta.scrollTop = ta.scrollHeight;
      }
   }

   function LOGStart(button){
      switch(button.value){
         case "Start log":
            CANLog.clearBufferOnServer();
            CANLog.start();
            button.value = "Stop";
            break;
         case "Stop":
            CANLog.stop();
            button.value = "Continue";
            break;
         case "Continue":
            CANLog.start();
            button.value = "Stop";
            break;
      }
   }

   function LOGFile(){
      //dir the files in the new window
      var win = window.open("ftp://" + location.hostname + "/A%3A/dump/", "dumpDirWin");

      var filename = prompt("Enter filename on the server!", "CANLog");
      if(filename){
         if(filename != "CANLog") filename = "dump/" + filename;
         var but = document.getElementById("LOGStartButton");
         if(but.value == "Stop"){
            CANLog.stop();
            but.value = "Continue";
         }
         var log = new CANLog_t(100000, 0, filename);
         log.registerCallbackBoundary(logBoundary);
      }
   }

   function emcy(filename){
      CO.readFile(filename, function(response){
         document.getElementById("EMCYlog").value = response;
      });
   }
   
   function CO_load(){
      CO = new CANopen_t();

      if(window.opener && typeof(window.opener.CANLog) == "object"){
         CANLog = window.opener.CANLog;
      }
      else{
         CANLog = new CANLog_t(5000, 500, 0);
      }
      CANLogCallbackID = CANLog.registerCallbackBoundary(logBoundary);
   }
   
   function CO_unload(){
      CANLog.removeCallback(CANLogCallbackID);
   }
   //]]>
</script>
</head>

<body onload="CO_load();" onunload="CO_unload();">
   <h3>CANopen configuration interface.</h3>
   <div>
      <fieldset class="oneBut"><legend>Send CAN message</legend>
         CAN ID (000 .. 7FF):<input type="text" id="sendID" value="230"/><br/>
         data (12 1A 05 00 ...):<input type="text" id="sendData" value="55 33"/><br/><br/>
         <input type="button" value="Send message" onclick="CO.sendCAN(document.getElementById('sendID').value, document.getElementById('sendData').value);"/>
      </fieldset>
      <br/>
      <fieldset class="twoBut"><legend>NMT master</legend>
         Node ID (0 for all nodes): <input type="text" id="NMTnodeId" value="0x30"/><br/><br/>
         <input type="button" value="Enter operational"     onclick="CO.sendNMT(document.getElementById('NMTnodeId').value, '01');"/> &nbsp;
         <input type="button" value="Reset communication"   onclick="CO.sendNMT(document.getElementById('NMTnodeId').value, '82');"/><br/>
         <input type="button" value="Enter pre-operational" onclick="CO.sendNMT(document.getElementById('NMTnodeId').value, '80');"/> &nbsp;
         <input type="button" value="Reset node"            onclick="CO.sendNMT(document.getElementById('NMTnodeId').value, '81');"/><br/>
         <input type="button" value="Enter stopped"         onclick="CO.sendNMT(document.getElementById('NMTnodeId').value, '02');"/> &nbsp;
      </fieldset>
      <br/>
      <fieldset class="threeBut"><legend>SDO master</legend>
         SDO addr <span style="font-size: 12px;">(NN IIII SS LLLL)</span>:<input type="text" id="SDOAddr" value="30 1017 00 0002"/><br/>
         Data (12 05 ...):<br/>
         <textarea id="SDOData">E8 03</textarea><br/>
         <div id="SDOStatus">SDO status Line.</div><br/>
         <input type="button" value="Read" onclick="SDORead();"/> &nbsp;
         <input type="button" value="Write" onclick="SDOWrite();"/> &nbsp;
         <input type="button" value="Info" onclick="alert('SDO address: 12 or 8 hex characters - NN = NodeId, IIII = Index, SS = Subindex, LLLL = length. Length is required only by write and must match length in data field. Valid length is 0001 to 0379. Whitespaces are ignored.\n\nData: Two hex characters for one byte. CANopen is Little Endian. Whitespaces are ignored.\n\nIn status line is indicated OK in case of successful read or write, otherwise AB with SDO abort code.');"/>
      </fieldset>
   </div>
   <div>
      <fieldset class="threeBut"><legend>CAN log</legend>
         <input type="button" value="Start log" onclick="LOGStart(this);" id="LOGStartButton"/> &nbsp;
         <input type="button" value="Dump" onclick="CANLog.dumpBufferOnServer();"/> &nbsp;
         <input type="button" value="Load file ..." onclick="LOGFile();"/><br/>
         <textarea id="CANLog">-</textarea>
      </fieldset>
   </div>
   <div>
      <fieldset class="threeBut"><legend>Emergency log</legend>
         <input type="button" value="Read last" onclick="emcy('emcy');"/> &nbsp;
         <input type="button" value="Flush" onclick="emcy('emcy?flush');"/> &nbsp;
         <input type="button" value="Read Log" onclick="emcy('emcy.log');"/><br/>
         <textarea id="EMCYlogDiv" id="EMCYlog">-</textarea>
      </fieldset>
   </div>
   <footer>
      <em><a href="http://canopennode.sourceforge.net/">CANopenNode</a></em>
   </footer>
</body>
</html>
